"use strict"

const xtpl=require("xtpl");
const orm=require("orm");
const fs=require("fs");
/*
	分页,根据连接URL中的参数,每次从数据库中只拿到当前页的数据,而页码是根据总条数计算的
 */
exports.videoPage=(req,res)=>{
	//获取浏览器传过来的参数
	//输入关键字搜索
  const keyword = req.query.keyword || "";
  const pageIndex=parseInt(req.query.pageindex||0);
  const pageSize=parseInt(req.query.pagesize||2);
  //计算出当前页考试的索引
  const offset=pageIndex*pageSize;
  //查询总条数
  req.models.video.count({},{},(err,count)=>{

  	const pagetotal=Math.ceil(count/pageSize);//计算出页数
  	let pages=[];
  	for(var i=0;i<pagetotal;i++){
  		pages.push(i);
  	}
  	console.log(pagetotal-1);
  	  //前往数据库中查找
		req.models.video.find({vtitle:orm.like("%"+keyword+"%")},{offset:offset,limit:pageSize},(err,videos)=>{
			xtpl.renderFile('./views/videolist.html',{
	    	uname:req.session.name,list:videos,keyword:keyword,pages:pages,pageIndex:pageIndex,pagetotal:pagetotal
			},function(err,content){
				if(err){
					console.log(err);
					return ;
				}
			res.setHeader("Content-Type","text/html;charset=utf-8");
		  res.end(content);
			});
		})
  })

	
}
exports.add=(req,res)=>{
	// fs.readFile("./views/add.html", (err,content)=>{
	// 	res.end(content);
	// })
	xtpl.renderFile('./views/add.html',{},function(err,content){
			if(err){
				console.log(err);
				return ;
			}
		res.setHeader("Content-Type","text/html;charset=utf-8");
	  res.end(content);
		});
}
// 增加页面,当用户点击保存的时候,首先通过input的name属性获取用户输入的内容,然后保存到数据库中去
exports.addpage=(req,res)=>{
	//获取参数,用户输入内容
  const vtitle = req.body.vtitle;
  const vsortno = req.body.vsortno;
  const vvideoid = req.body.vvideoid;
  const vsummary = req.body.vsummary;
  const vimgurl = req.body.vimgurl;
  const vremark = req.body.vremark;
  console.log("----新增");
  req.models.video.create([{
        vtitle:vtitle,
        vsortno:vsortno,
        vvideoid:vvideoid,
        vsummary:vsummary,
        vimgurl:vimgurl,
        vremark:vremark
    }],(err)=>{
     res.setHeader("Content-Type","text/html;charset=utf-8");
     res.end("<script>alert('新增成功!!!');window.location.href='/video/list';</script>");
    });

}
/*
	编辑内容:当用户点击,触发点击事件,修改window.href跳转至编辑页面,同时url传参,要修改的id,后台根据ID找到数据库中对应的内容,然后进行修改
 */
// 首先获取编辑的页面
exports.editor=(req,res)=>{
	//获取要编辑的ID
	const vid=parseInt(req.params.vid||0);
	//根据ID去数据中查找
	req.models.video.get(vid,(err,video)=>{
    xtpl.renderFile('./views/editor.html',{video:video},(err,content)=>{
        res.setHeader("Content-Type","text/html;charset=utf-8");
    
        res.end(content);
    });
    })
}
//提交编辑内容
exports.editorpage=(req,res)=>{
	    //获取提交过来的参数
    const vid = parseInt(req.params.vid || 0);

    const vtitle = req.body.vtitle;
    const vsortno = req.body.vsortno;
    const vvideoid = req.body.vvideoid;
    const vsummary = req.body.vsummary;
    const vimgurl = req.body.vimgurl;
    const vremark = req.body.vremark;

    //先查后改
    req.models.video.get(vid,(err,video)=>{
        video.vtitle = vtitle;
        video.vsortno = vsortno;
        video.vvideoid = vvideoid;
        video.vsummary = vsummary;
        video.vimgurl = vimgurl;
        video.vremark = vremark;

        video.save((err)=>{
            if (err) {
                console.log(err);
                return;
            }
            res.setHeader("Content-Type","text/html;charset=utf-8");
            res.end("<script>alert('修改成功!!!');window.location.href='/video/list';</script>");
        });
    })
}

/*
	删除功能: 不需要跳转页面了.直接发送请求,传递ID,后台拿到ID,在数据中进行删除
 */
exports.delete=(req,res)=>{
	//获取参数
    const vid = parseInt(req.params.vid || 0);

    //{"status":"1","message":"删除成功"}  //1代表成功 0 代表失败

    const returnObj = {"status":"1","message":"删除成功"};

    //先找到它,再删除
    req.models.video.get(vid,(err,video)=>{
        video.remove((err)=>{
            if (err) {
                returnObj.status = "0";
                returnObj.message = "删除失败";
                return;
            }

            res.end(JSON.stringify(returnObj));
        });
    });
}